﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Bedroomss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.PigFarmss
{
    public class AddFarmHander : IRequestHandler<AddFarmCommand, Result>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Pigfarmas> pigfarmsrepository;

        public AddFarmHander(IMapper mapper, IBaseRepository<Pigfarmas> pigfarmsrepository)
        {
            this.mapper = mapper;
            this.pigfarmsrepository = pigfarmsrepository;
        }
        /// <summary>
        /// 猪场添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddFarmCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Pigfarmas entity = mapper.Map<Pigfarmas>(request);
            var res = await pigfarmsrepository.Add(entity);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
            }
            result.Msg = "添加成功";
            return result;
        }


    }
}
